package com.module.ble.ui.colorSelection

import android.graphics.Color
import android.util.Log
import android.view.View
import com.alibaba.android.arouter.facade.annotation.Route
import com.common.app.core.RouterHub
import com.common.app.data.bean.BaseCityModel
import com.common.app.utls.ColorUtils.colorToHexStringWithAlpha
import com.common.app.utls.HeadTitleUtils
import com.common.base.app.activity.BaseNavActivity
import com.common.base.app.extras.setOnClickListener
import com.common.base.utils.StatusBarUtils
import com.module.ble.R
import com.module.ble.databinding.BleActivityColorSelectionBinding
import com.module.ble.databinding.BleActivityColorSelectionOldBinding
import com.module.ble.widget.CusColorPickView.OnColorChangedListener
import com.module.ble.widget.customseekbar.IndicatorSeekBar


/**
 * 作者：sosou
 * <p>
 * 版本：1.0
 * <p>
 * 创建日期：2025/09/29
 * <p>
 * 描述：颜色选择页面
 * <p>
 * 修订历史：
 */
//@Route(path = RouterHub.ROUTER_SELECT_COLOR)
class ColorSelectionActivityOld : BaseNavActivity<BleActivityColorSelectionOldBinding>(),
    View.OnClickListener {

    override fun getViewContentBinding() = BleActivityColorSelectionOldBinding.inflate(layoutInflater)

    override fun getPlaceholderView() = mViewBinding.mPlaceholderView

    override fun isShowToolBar(): Boolean = false
    override fun isStatusBarTranslate(): Boolean = true

    private var currentColorValue = 0
    private var adjustedColorValue = 0 // 调整后的颜色

    private val lightingEffectConfigDialog by lazy { CommonLightingEffectConfigDialog(this) }

    override fun initViews() {
        super.initViews()
        StatusBarUtils.setStatusBarHeight(mViewContentBinding.statusView)
        StatusBarUtils.setStatusBarForegroundColor(this, true)

        val headTitleUtils = HeadTitleUtils(this)
        headTitleUtils.setTitle(getString(R.string.ble_color_picker_title))
        headTitleUtils.setLineStyle(10)
    }

    override fun initEvents() {
        super.initEvents()

        mViewContentBinding.cpdView.setOnColorChangedListener(object : OnColorChangedListener {

            override fun onColorChange(
                color: Int,
                red: Int,
                green: Int,
                blue: Int
            ) {
                currentColorValue = color

                // 获取颜色的HSV值
                val hsv = FloatArray(3)
                Color.colorToHSV(currentColorValue, hsv)

                val hue = hsv[0]        // 色相 (0-360)
                val saturation = hsv[1] // 饱和度 (0.0-1.0)
                val brightness = hsv[2] // 亮度 (0.0-1.0)

                // 转换为百分比
                val brightnessPercent = (brightness * 100).toInt() // 0-100

                Log.d("ColorSelection", "色相: $hue, 饱和度: ${(saturation * 100).toInt()}%, 亮度: $brightnessPercent%")

                // 同步更新亮度滑动条的位置
//                mViewContentBinding.inbBrightness.setProgress(brightnessPercent.toFloat())

                mViewContentBinding.tvColorValue.text = "${getString(R.string.ble_color_hue_hint)}  ${colorToHexStringWithAlpha(currentColorValue)}"
                mViewContentBinding.cvColorValue.setCardBackgroundColor(currentColorValue)
            }
        })

        mViewContentBinding.inbBrightness.setOnSeekChangeListener(object : IndicatorSeekBar.OnSeekBarChangeListener{
            override fun onProgressChanged(
                seekBar: IndicatorSeekBar?,
                progress: Int,
                progressFloat: Float,
                fromUserTouch: Boolean
            ) {
//                // 使用 HSV 颜色模型调整亮度
//                val hsv = FloatArray(3)
//                Color.colorToHSV(currentColorValue, hsv)
//                // 调整亮度值 (V值)，progress 0-100 对应亮度 0.0-1.0
//                hsv[2] = progress / 100f
//                adjustedColorValue = Color.HSVToColor(Color.alpha(currentColorValue), hsv)
//                mViewContentBinding.tvColorValue.text = "${getString(R.string.ble_color_hue_hint)}  ${colorToHexStringWithAlpha(adjustedColorValue)}"
//                mViewContentBinding.cvColorValue.setCardBackgroundColor(adjustedColorValue)

                mViewContentBinding.tvBrightnessValue.text = "${getString(R.string.ble_color_brightness_hint)}  ${progress}%"
            }

            override fun onSectionChanged(
                seekBar: IndicatorSeekBar?,
                thumbPosOnTick: Int,
                textBelowTick: String?,
                fromUserTouch: Boolean
            ) {
            }

            override fun onStartTrackingTouch(
                seekBar: IndicatorSeekBar?,
                thumbPosOnTick: Int
            ) {
            }

            override fun onStopTrackingTouch(seekBar: IndicatorSeekBar?) {
            }

        })

        mViewContentBinding.inbVelocity.setOnSeekChangeListener(object : IndicatorSeekBar.OnSeekBarChangeListener{
            override fun onProgressChanged(
                seekBar: IndicatorSeekBar?,
                progress: Int,
                progressFloat: Float,
                fromUserTouch: Boolean
            ) {
                mViewContentBinding.tvVelocityValue.text = "${getString(R.string.common_velocity_hint)}  $progress"
            }

            override fun onSectionChanged(
                seekBar: IndicatorSeekBar?,
                thumbPosOnTick: Int,
                textBelowTick: String?,
                fromUserTouch: Boolean
            ) {
            }

            override fun onStartTrackingTouch(
                seekBar: IndicatorSeekBar?,
                thumbPosOnTick: Int
            ) {
            }

            override fun onStopTrackingTouch(seekBar: IndicatorSeekBar?) {
            }

        })

        lightingEffectConfigDialog.setListener(object : CommonLightingEffectConfigDialog.OnSelectListener{
            override fun onSelectClick(
                mType: Int,
                mBean: BaseCityModel
            ) {

            }

        })

        mViewContentBinding.btnConfirm.setOnClickListener(onClick = this)
    }

    override fun lazyInitData() {
        super.lazyInitData()
    }

    override fun onClick(v: View?) {
        finish()
    }

    override fun onStart() {
        super.onStart()
    }

    override fun onStop() {
        super.onStop()
    }
    override fun onDestroy() {
        super.onDestroy()
    }
}